var router = require('express').Router();
var util = require('util');
var uuid = require('node-uuid');
var cookie = require('cookie');
var LRU = require('lru-cache');
var EventProxy = require('eventproxy');
var async = require('async');
var _ = require('underscore');

var CONSTS = require('../consts');
var logger = require('../models/logger');
var User = require('../models/User');
var resultJson = require('../result-json');
var utilsHelper = require('../utils/utilsHelper');
var dateHelper = require('../utils/dateHelper');
var downloadHelper = require('../utils/downloadHelper');
var nodeExcel = require('../utils/xlsExportHelper');
var smsHelper = require('../utils/smsHelper');
var cookieHelper = require('../utils/cookieHelper');
var dbHelper = require('../utils/dbHelper');

/*注册用户*/
router.post('/upsert',async function(req,res) {
	var params = req.body;
	if (!params.pwd) {
		return res.send(resultJson(3));
	}
	var _user = await User.findOne({
				$or: [{
					phone: params.phone
				}, {
					name: params.name
				}],
			});
	if (_user) {
		return res.send(resultJson(102));
	}
	if (!params._id) {
		new User(params).save(function(err, item, cb) {
                if (err) {
                    logger.error(err);
                    return res.send(resultJson(dbHelper.dataDuplicate(err)));
                }
            }).then(function() {
            	return res.send(resultJson(0, params));
            })
	} else {
		User.update({
                _id: params._id
            }, {
                $set: params
            }).exec(function(err, results) {
                if (err) {
                    logger.error(err);
                    return res.send(resultJson(dbHelper.dataDuplicate(err)));
                }
                return res.send(resultJson(0));
            });
	}
	
})
/*登录*/
router.post('/lgn',function(req,res) {
	var params = req.body;
	if (!params.pwd || !params.phone) {
		return res.send(resultJson(3));
	}
	User.find(params, function(err, users){
		logger.debug('user find, item:',params)
		if (err || users.length == 0) {
			if (err) {
				return res.send(resultJson(2));
			}
			if (pwd) {
				return res.send(resultJson(100));
			}
		} else {
			users = JSON.parse(JSON.stringify(users));
			var id;
			id = users[0]._id;
			cookieHelper.setCookie(id, res);
			var results = {
				"aaa": "123456",
				"result": users[0]
			}
			return res.send(resultJson(0, results));
		} 
	})
})

/*列表详情*/
router.post('/list', function(req, res) {
	var proxy = new EventProxy();
	proxy.all('results', 'count', function(results, count) {
		var result = {
			results: results,
			count: count
		}
		return res.send(resultJson(0, result));
	})
	User.find({}).exec(function(err, items) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        proxy.emit('results', items);
    });
    User.find({}).count(function(err, num) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        proxy.emit('count', num);
    });
})

module.exports = router;
